<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"/>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>

<div th:insert="head.html"></div>



<div class="container-fluid" id="app" style="margin-top: 180px;margin-bottom: 100px">


    <div class="container">

        <div class="row">
            <a href="/register.html">还没有账号?点我立即去注册</a>
            <div class="col-md-4 col-md-offset-4" style="border: 1px solid #cccccc ;border-radius: 5px;margin-top: 10px">
                <p style="color: red" v-text="errorInfo"></p>
                <form>
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <input type="text" v-model="loginForm.username" name="username" class="form-control" id="username" placeholder="请输入用户名">
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" v-model="loginForm.password" name="password" class="form-control" id="password" placeholder="请输入密码">
                    </div>

                    <div class="form-group">
                        <label for="code">验证码</label>
                        <input type="text" v-model="loginForm.code" name="code" class="form-control" id="code" placeholder="请输入验证码">
                        <a href="" @click.prevent="refreshKaptchaCode"><img src="/user/getImgCode" alt="..." id="codeImg" />点击刷新</a>
                    </div>

                    <button class="btn btn-success" @click.prevent="toLogin">登 录</button>
                </form>
            </div>
        </div>

    </div>



</div>


<script>

    let app = new Vue({
        el:"#app",
        data:{
            loginForm:{
                username:'',
                password:'',
                code:''
            },
            errorInfo:''
        },
        methods:{
            refreshKaptchaCode(){
                let src = "/user/getImgCode?date="+new Date().valueOf();
                $("#codeImg").attr("src",src)
            },
            toLogin(){
                if (this.loginForm.code==='' && this.loginForm.password==='' && this.loginForm.username===''){
                    this.errorInfo = '缺少必要的信息'
                    return
                }

                $.post("/user/login",this.loginForm,function (data) {
                    if(data==="200"){
                        location.href="/"
                    }else {
                        alert(data)
                    }
                })


            }

        },
        created(){

        }
    })

</script>


<div th:insert="foot.html"></div>


</body>
</html>